package com.gcb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcb.common.constant.CommonConstants;
import com.gcb.common.core.domain.entity.GcbUser;
import com.gcb.common.enums.*;
import com.gcb.common.exception.ServiceException;
import com.gcb.common.utils.PasswordUtils;
import com.gcb.domain.dto.CrewQueryDTO;
import com.gcb.domain.dto.CrewSaveDTO;
import com.gcb.domain.entity.Crew;
import com.gcb.domain.vo.CrewVO;
import com.gcb.mapper.CrewMapper;
import com.gcb.service.ICrewService;
import com.gcb.service.IGcbUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CrewServiceImpl extends ServiceImpl<CrewMapper, Crew> implements ICrewService {

    @Resource
    private CrewMapper crewMapper;

    @Resource
    @Lazy
    private IGcbUserService gcbUserService;

    @Override
    public List<CrewVO> pageList(CrewQueryDTO dto) {
        List<CrewVO> crewVOS = crewMapper.pageList(dto);
        crewVOS.forEach(this::handleVO);
        return crewVOS;
    }

    private void handleVO(CrewVO crewVO) {
        if (crewVO.getCredentialsType() != null) {
            crewVO.setCredentialsTypeStr(CredentialsTypeEnum.getInfo(crewVO.getCredentialsType()));
        }
        if (crewVO.getCredentialsLevel() != null) {
            crewVO.setCredentialsLevelStr(CredentialsLevelEnum.getInfo(crewVO.getCredentialsLevel()));
        }
        crewVO.setCreateTypeStr(CreateTypeEnum.getInfo(crewVO.getCreateType()));
        crewVO.setAuthStatusStr(AuthStatusEnum.getInfo(crewVO.getAuthStatus()));
    }

    @Override
    public void add(CrewSaveDTO dto) {
        // 根据手机号查询用户，没有新增
        Boolean checkAuth = crewMapper.checkAuth(dto.getMobile());
        if (checkAuth) {
            log.error("当前手机号：{} 船员已认证，无需重复认证", dto.getMobile());
            throw new ServiceException("当前手机号：" + dto.getMobile() + " 船员已认证，无需重复认证");
        }
        Long userId;
        if (gcbUserService.getByMobile(dto.getMobile()) == null) {
             userId = this.addGcbUser(dto);
        }else{
            userId = gcbUserService.getByMobile(dto.getMobile()).getId();
        }
        Crew crew = new Crew();
        BeanUtil.copyProperties(dto, crew);
        crew.setUserId(userId);
        crew.setAuthStatus(AuthStatusEnum.VERIFIED.getCode());
        crew.setAuthTime(new Date());
        crew.setCreateBy(dto.getOperatorId());
        crew.setUpdateBy(dto.getOperatorId());
        crewMapper.insert(crew);
    }

    /**
     * 添加工船宝用户
     * @param dto
     * @return
     */
    private Long addGcbUser(CrewSaveDTO dto){
        GcbUser gcbUser = new GcbUser();
        gcbUser.setMobile(dto.getMobile());
        gcbUser.setName(dto.getName());
        //默认密码手机号后6位
        gcbUser.setPassword(PasswordUtils.getDefaultAppUserPassword(dto.getMobile()));
        // 状态 1正常
        gcbUser.setStatus(CommonConstants.GcbUser.STATE_NORMAL);
        gcbUser.setIdentityCard(dto.getIdCard());
        gcbUser.setAge(IdcardUtil.getAgeByIdCard(dto.getIdCard()));
        if(IdcardUtil.getGenderByIdCard(dto.getIdCard())==1){
            gcbUser.setSex(SexTypeEnum.MAN.getCode());
        }else {
            gcbUser.setSex(SexTypeEnum.WOMAN.getCode());
        }
        gcbUser.setProvince(IdcardUtil.getProvinceCodeByIdCard(dto.getIdCard()));
        gcbUser.setCity(IdcardUtil.getCityCodeByIdCard(dto.getIdCard()));
        gcbUser.setAuthStatus(AuthStatusEnum.AUDITING.getCode());
        gcbUser.setAuthTime(new Date());
        gcbUser.setSignType(CreateTypeEnum.OPERATION.getCode());
        gcbUser.setCreateBy(dto.getOperatorId());
        gcbUserService.save(gcbUser);
        return gcbUser.getId();
    }

    @Override
    public void update(CrewSaveDTO dto) {
        Crew crew = new Crew();
        BeanUtil.copyProperties(dto, crew);
        crew.setUpdateBy(dto.getOperatorId());
        crewMapper.updateById(crew);
    }

    @Override
    public CrewVO detail(Long id) {
        CrewQueryDTO dto = new CrewQueryDTO();
        dto.setId(id);
        CrewVO crewVO = crewMapper.selectOne(dto);
        handleVO(crewVO);
        return crewVO;
    }

    @Override
    public CrewVO getByUserId(Long userId) {
        CrewQueryDTO dto = new CrewQueryDTO();
        dto.setUserId(userId);
        CrewVO crewVO = crewMapper.selectOne(dto);
        handleVO(crewVO);
        return crewVO;
    }

    @Override
    public Boolean checkAuth(String mobile) {
        return crewMapper.checkAuth(mobile);
    }
}
